﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using NCKH.models;
namespace Test
{
    public class Methods
    {
        public static List<string> Times = new List<string>();
        public static int Space = new int();
        public static void WriteHoursList(string pathFile, XmlDocument xmlDoc)
        {
            Times.Clear();

            DataContext db = new DataContext();

           
              var  chtg = new EXM_CauHinhThoiGian
                {
                    Kieu_thoi_gian = "120",
                    Ngay_thoi_gian = "1111110",
                    Thoi_gian_bat_dau = "0730,0930,0000,1330,1530"
                };
            

            string[] hours = chtg.Thoi_gian_bat_dau.Split(',');

            for (int i = 0; i < hours.Length; i++)
                hours[i] = hours[i].Insert(2, ":");

            int space = Space = int.Parse(chtg.Kieu_thoi_gian.Substring(2));

            //----Hours

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Hours_List");
            {
                XmlNode nodeNumber = xmlDoc.CreateNode(XmlNodeType.Element, "Number", null);
                nodeNumber.InnerText = ((hours.Length > 11 ? 11 : hours.Length)).ToString();
                root.AppendChild(nodeNumber);
                for (int i = 0; i < (hours.Length > 11 ? 11 : hours.Length); i++)
                {
                    if (hours[i].Equals("00:00"))
                    {
                        XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        node.InnerText = "Nghỉ";
                        root.AppendChild(node);
                        //-----------------------------------------------------
                        Times.Add("Nghỉ");
                    }
                    else
                    {
                        XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        TimeSpan ts = TimeSpan.Parse(hours[i]);
                        string hour = (DateTime.Parse(ts.ToString())).ToString("HH:mm");
                        ts += (new TimeSpan(0, space, 0));
                        hour = String.Format("{0} - {1}", hour, (DateTime.Parse(ts.ToString())).ToString("HH:mm"));
                        node.InnerText = hour;
                        root.AppendChild(node);
                        //-----------------------------------------------------
                        Times.Add(hour);
                    }
                }
            }
            xmlDoc.Save(pathFile);
        }
    }
}
